package com.water.devtool.define;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.water.devtool.constant.BeanType;
import com.water.util.JsonUtil;
import com.watermud.util.Function;
import com.watermud.util.IteratorContext;
import com.watermud.util.IteratorUtil;

/**
 * 工程
 * 
 * @author sun.lihua
 *
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProjectBean extends DefineBean {
	private String code;
	private String desc;
	private Map<String, ModularBean> modulars = new HashMap<String, ModularBean>();
	private String packageNameStyle = "com.wckj.{#project}.{#modular}.{#classLevel}";
	private String classNameStyle = "{#entityJavaName}{#levelName?caption}";
	private String defaultDataSource;
	private String defaultSeqName;
	private DbConfigBean dbConfig;
	private Map<String, LevelConfig> levelConfig;
	private int versionCode ;

	@JsonIgnore
	private String projectPath;//通过conf配置workspace，再加上项目名，就是projectPath
	private String srcRootPath;

	public void init() {
		if (levelConfig == null) {
			levelConfig = LevelConfig.defaultConfig();
		}
		if(modulars==null) {
			return ;
		}
		Set<String> keys = modulars.keySet();
		for (String key : keys) {
			ModularBean modualr = modulars.get(key);
			modualr.setProject(this);
			modualr.init();
		}
	}

	public List<LevelConfig> listLevelConfig() {
		if (levelConfig == null) {
			return null;
		}
		List<LevelConfig> list = new ArrayList<LevelConfig>(levelConfig.values());
		Collections.sort(list, new Comparator<LevelConfig>() {

			@Override
			public int compare(LevelConfig o1, LevelConfig o2) {
				return o1.getOrderNo() - o2.getOrderNo();
			}
		});
		return list;
	}

	@Override
	@JsonIgnore
	public BeanType getType() {
		return BeanType.PROJECT;
	}

	public void addModular(ModularBean modular) {
		if (modulars == null) {
			modulars = new HashMap<String, ModularBean>();
		}
		modulars.put(modular.getCode(), modular);
	}

	public void deleteModular(ModularBean modular) {
		modulars.remove(modular.getCode());
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getDefaultDataSource() {
		return defaultDataSource;
	}

	public void setDefaultDataSource(String defaultDataSource) {
		this.defaultDataSource = defaultDataSource;
	}

	public String getPackageNameStyle() {
		return packageNameStyle;
	}

	public void setPackageNameStyle(String packageNameStyle) {
		this.packageNameStyle = packageNameStyle;
	}

	public String getClassNameStyle() {
		return classNameStyle;
	}

	public void setClassNameStyle(String classNameStyle) {
		this.classNameStyle = classNameStyle;
	}

	public String getDefaultSeqName() {
		return defaultSeqName;
	}

	public void setDefaultSeqName(String defaultSeqName) {
		this.defaultSeqName = defaultSeqName;
	}

	public Map<String, ModularBean> getModulars() {
		return modulars;
	}

	public void setModulars(Map<String, ModularBean> modulars) {
		this.modulars = modulars;
	}

	public DbConfigBean getDbConfig() {
		return dbConfig;
	}

	public void setDbConfig(DbConfigBean dbConfig) {
		this.dbConfig = dbConfig;
	}

	public Map<String, LevelConfig> getLevelConfig() {
		return levelConfig;
	}

	public void setLevelConfig(Map<String, LevelConfig> levelConfig) {
		this.levelConfig = levelConfig;
	}

	public LevelConfig getLevelConfig(String level) {
		return levelConfig.get(level);
	}

	public String getSrcRootPath() {
		return srcRootPath;
	}

	public void setSrcRootPath(String srcRootPath) {
		this.srcRootPath = srcRootPath;
	}

	public String getProjectPath() {
		return projectPath;
	}

	public void setProjectPath(String projectPath) {
		this.projectPath = projectPath;
	}

	public Map<String, String> toMulString() {
		Map<String, String> map = new HashMap<String, String>();
		String dbJson = JsonUtil.toJson(dbConfig);
		map.put("dbConfig", dbJson);
		IteratorUtil.iterate(modulars, new Function<ModularBean>() {

			@Override
			public void execute(IteratorContext context, ModularBean m) {
				String json = JsonUtil.toJson(m);
				map.put("modular." + m.getCode(), json);
			}

		});

		map.put("projectPath", projectPath == null ? "" : projectPath);

		DbConfigBean oldDb = dbConfig;
		dbConfig = null;
		Map<String, ModularBean> oldModulars = modulars;
		modulars = null;
		String oldProjectPath = projectPath;
		projectPath = null;
		String projectJson = JsonUtil.toJson(this);
		map.put("project", projectJson);

		dbConfig = oldDb;
		modulars = oldModulars;
		projectPath = oldProjectPath;
		return map;

	}

	public int getVersionCode() {
		return versionCode;
	}

	public void setVersionCode(int versionCode) {
		this.versionCode = versionCode;
	}

	
	
}
